/**

 * 
 */
//创建日期显示
var d=new Date();
var t=d.toLocaleString();
$("#createDate").text(t);
	
var curMonth=new Date().getMonth()+1;		//获取当前用户指定月份，默认是当前自然月份
var curYear=new Date().getFullYear();		//获取当前用户指定年份，默认是当前自然年份	
var curDate=new Date().getDate();			//获取当前用户指定日期，默认是当前自然日期
init();

getYearMonth(curYear,curMonth);

// 加载日历div元素
function init() {
	var monthDate = "";
	for (var i = 1; i < 36; i++) {
		if (i % 7 == 0) {
			monthDate += "<div class='myDate seven'></div>";
		} else {
			monthDate += "<div class='myDate'></div>";
		}
	}
	for(var i=1;i<7;i++){
		monthDate += "<div class='myDate calBottom'></div>";
	}
	monthDate+="<div class='myDate seven calBottom'></div>";
	monthDate += "<div style='clear:both;'></div>";
	$(".dates").eq(0).empty().append(monthDate);
	getCalendar(new Date(curYear,curMonth-1,curDate));
}

// 添加日历方法
function getCalendar(myDate) {
	var dates = getMonthDays(myDate.getFullYear(),myDate.getMonth());
	// console.log("当月天数",dates);
	// console.log("今天日期",date.getDate());
	myDate.setDate(1);
	// var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
	var dateStart = myDate.getDay();
	// console.log("日期开始数",dateStart);
	var myDates = $(".myDate");
	myDates.each(function(){
		$(this).text('');
	})
	//获取当前日历所有日程
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/UserSchedules',
		data:{"userName":"黄永"},
		success:function(data){
//			console.log(data);
			var schedules=JSON.parse(data);
			
			for (var i = dateStart, j = 1; j <= dates; i++, j++) {
				var content="";
				
				if(j<10){
					content+="<div>&nbsp;<span>"+j+"</span>&nbsp;&nbsp;&nbsp;&nbsp;<i class='fa fa-calendar-check-o' onclick='getCalDetail(this)'></i></div>";
				}else{
					content+="<div>&nbsp;<span>"+j+"</span>&nbsp;&nbsp;<i class='fa fa-calendar-check-o' onclick='getCalDetail(this)'></i></div>";
				}
				
				if(j==new Date().getDate()){
					$(".myDate").eq(i).addClass('luckyDay');
				}
				for(var k=0;k<schedules.length;k++){
//					console.log(schedules[k].beginTime);
					calDate=schedules[k].beginTime.slice(8,10);
					if(calDate==j){
						content+="<div><span class='todaySchedules'><i class='fa fa-tasks fa-lg'></i>"+schedules[k].beginTime.slice(11,16)+"&nbsp;"+schedules[k].title.slice(0,3)+"...</span></div>";
					}
				}
				
				myDates.eq(i).empty().append(content);
			}
			
		},
		error:function(res){
			console.log(res)
		}
	});
}

//获取上一月日历
function getPreMonth(){
	curMonth--;
	if(curMonth<1){
		curMonth=12;
		curYear=curYear-1;
	}					
	var curDates=new Date(curYear,curMonth-1,curDate);		
	getYearMonth(curYear,curMonth);	
	getCalendar(curDates);		
	console.log("点击上一月后的参照月",curMonth);		
}

//获取下一月日历
function getNextMonth(){
	curMonth++;
	if(curMonth>12){
		curMonth=1;
		curYear=curYear+1;
	}
	var curDates=new Date(curYear,curMonth-1,curDate);		
	getYearMonth(curYear,curMonth);	
	getCalendar(curDates);		
	console.log("点击下一月后的参照月",curMonth);	
}

// 获取一个月天数
function getMonthDays(year,month) {
	var d = new Date(year, month+1, 0);
	return d.getDate();
}

//获取指定日期年月,默认为自然年月
function getYearMonth(currentYear,currentMonth) {
	$(".thisMonth").eq(0).text(currentYear + "年 " + currentMonth + "月");
}

//展示日程细节窗体
function getCalDetail(i){
	//获取年份
	console.log(curYear);
	//获取月份
	console.log(curMonth);
	//获取日期
	console.log($(i).prev().text());
	
	//获取会议类型
	getMeetingType();
	//获取所有用户信息
	//获取创建者姓名（当前用户名）
	//获取创建日期（当前日期）
	$("#calendarDetailModal").css({
		"top": "10vh",
		"transition": "top .3s ease",
	});
	$("#cover").css("display", "block").attr("onclick", "coverBack('#calendarDetailModal')");	
}

//获取会议类型
function getMeetingType(){
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/meetinginfo',
		data:{
			"typeName":"MeetingInfo",
			"method":"getPageDatas"
		},
		success:function(data){
//			console.log(data);
			var meetingType=JSON.parse(data);
			var types="";
			for(var i=0;i<meetingType.length;i++){
				types+="<option value='"+meetingType[i].meetingId+"'>"+meetingType[i].meetingName+"</option>";
			}
			$("#myMeetingType").empty().append(types);
		},
		error:function(res){
			console.log(res);
		}
	})
}

//添加日程
$("#addScheduleBtn").click(function(){	
//	0: input.scheduleAdded
//	1: input.scheduleAdded
//	2: select#myMeetingType.scheduleAdded
//	3: input.scheduleAdded
//	4: input.scheduleAdded
//	5: textarea.calDetailSpan.scheduleAdded
//	6: textarea.calDetailSpan.scheduleAdded
//	7: input.scheduleAdded
//	8: span.scheduleAdded
//	9: span.scheduleAdded
//	length: 10
	
	//获取用户输入
	var schedules=$(".scheduleAdded");
//	console.log(schedules);
	//装入数组
	var schdArr=[];
	for(var i=0;i<schedules.length-3;i++){		
		schdArr.push(schedules[i].value);
	}
	
	console.log($(schedules[6]).is(":checked"));
	schdArr.push($(schedules[6]).is(":checked"));
	schdArr.push(schedules.eq(7).text());
	schdArr.push(schedules.eq(8).text());
//	console.log(schdArr);
//	组装schedule对象
//	这里要做字符串和日期的转换处理
	
	var schedules={
			"title":schdArr[0],
			"address":schdArr[1],
			"meetingId":schdArr[2],
			"schContent":schdArr[5],
			"ifPrivate":schdArr[6]==true?0:1,
			"createUser":schdArr[7],
	}
//	组装preContact对象
	var preContact={
			"userId":4
	}

	console.log(schdArr[3]);
	console.log(schdArr[4]);
	//执行添加
	$.ajax({
		type:'post',
		url:'http://localhost:8088/myOA/schedules',
		data:{
			"method":"addSchedule",
			"schedule":JSON.stringify(schedules),
			"preContact":JSON.stringify(preContact),
			"beginTime":"2020/08/16 "+schdArr[3]+":00",
			"endTime":"2020/08/16 "+schdArr[4]+":00",
			"createTime":'2020/08/16 04:28:09'		//schdArr[8]
		},
		success:function(data){
			var result=JSON.parse(data);
			if(result.status=="ok"){
				alert("添加成功");
				//成功后，关闭弹窗
				coverBack($("#calendarDetailModal"))
				//再次添加日历
				getCalendar();				
			}
		},
		error:function(res){
			console.log(res);
		}
	});
});











